// index.js
Page({
  data: {
    banners: [
      {
        image: "/assets/proto-img/轮播图1.jpg",
        title: "城旅社区欢迎你",
        //link: "/pages/find/index"
      },
      {
        image: "/assets/proto-img/轮播图2.jpg", 
        title: "发现城市美好",
        //link: "/pages/circle/index"
      },
      {
        image: "/assets/proto-img/轮播图3.jpg",
        title: "热门推荐",
        //link: "/pages/circle/hot/index"
      },
      {
        image: "/assets/proto-img/轮播图4.jpg",
        title: "城市排行榜",
        //link: "/pages/rank/index"
      }
    ],
    oprs: [
      {
        txt: "本地攻略",
        icon: "/assets/img/find/本地攻略2.png",
        link: "/pages/guide/index"
      },
      {
        txt: "热门商家",
        icon: "/assets/img/find/热门商家.png",
        link: "/pages/shop/index"
      },
      {
        txt: "城市活动",
        icon: "/assets/img/find/城市活动.png",
        link: "/pages/activity/index"
      },
      {
        txt: "排行榜",
        icon: "/assets/img/find/排行榜.png",
        link: "/pages/rank/index"
      },
      {
        txt: "任务中心",
        icon: "/assets/img/find/任务中心1.png",
        link: "/pages/task/index"
      }
    ],
    dynamics: [
      {
        id: 1,
        img: "/assets/img/message/社区动态1.jpg",
        title: "周末城市徒步路线推荐，沿江风景一览",
        type: "攻略",
        comments: 128,
        stars: 532,
        author: "城旅达人",
        publishTime: "2024-01-15 10:30",
        tags: ["徒步", "风景", "周末"]
      },
      {
        id: 2,
        img: "/assets/img/message/社区动态2.jpg",
        title: "本地商家优惠合集，吃喝玩乐一站式",
        type: "资讯",
        comments: 89,
        stars: 421,
        author: "美食推荐官",
        publishTime: "2024-01-14 16:20",
        tags: ["美食", "优惠", "本地"]
      },
      {
        id: 3,
        img: "/assets/img/message/社区动态3.jpg",
        title: "社区志愿者招募，和你一起让城市更美好",
        type: "公告",
        comments: 56,
        stars: 310,
        author: "城旅社区",
        publishTime: "2024-01-13 09:15",
        tags: ["志愿者", "公益", "社区"]
      },
      {
        id: 4,
        img: "/assets/img/message/社区动态4.jpg",
        title: "数字城旅发布：城市服务数字地图上线",
        type: "新闻",
        comments: 203,
        stars: 860,
        author: "城旅官方",
        publishTime: "2024-01-12 14:45",
        tags: ["数字地图", "服务", "官方"]
      }
    ],
    boardData: [
      {
        image: "/assets/img/message/精品推荐1.jpg",
        likeCount: 128,
        avatar: "/assets/img/profile/profile.png",
        username: "城旅小助手",
        content: "发现城市美好，分享生活点滴",
        time: "2024-01-15 14:30"
      },
      {
        image: "/assets/img/message/精品推荐2.jpg",
        likeCount: 89,
        avatar: "/assets/img/profile/account.png",
        username: "美食达人",
        content: "今天发现了一家超棒的咖啡店！",
        time: "2024-01-15 12:20"
      },
      {
        image: "/assets/img/message/精品推荐3.jpg",
        likeCount: 256,
        avatar: "/assets/img/profile/medal.png",
        username: "摄影爱好者",
        content: "城市夜景真的太美了，推荐大家去打卡",
        time: "2024-01-15 08:45"
      },
      {
        image: "/assets/img/message/精品推荐4.jpg",
        likeCount: 167,
        avatar: "/assets/img/profile/collect.png",
        username: "旅行者",
        content: "周末徒步路线分享，风景绝美！",
        time: "2024-01-14 18:30"
      },
      {
        image: "/assets/img/message/精品推荐5.jpg",
        likeCount: 98,
        avatar: "/assets/img/profile/gift.png",
        username: "运动达人",
        content: "晨跑发现的新路线，空气清新",
        time: "2024-01-14 07:15"
      },
      {
        image: "/assets/img/message/精品推荐6.jpg",
        likeCount: 203,
        avatar: "/assets/img/profile/tag.png",
        username: "本地通",
        content: "推荐几个隐藏的美食小店",
        time: "2024-01-13 20:10"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showToast({
      title: '刷新成功',
      icon: 'success'
    })
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '城旅社区 - 发现城市美好',
      path: '/pages/index/index'
    }
  },

  /**
   * 处理动态点赞
   */
  onDynamicLike: function(e) {
    const { id, stars } = e.detail
    const dynamics = this.data.dynamics.map(item => {
      if (item.id === id) {
        item.stars = stars
      }
      return item
    })
    this.setData({ dynamics })
  },

  /**
   * 处理推荐卡片点赞
   */
  onBoardLike: function(e) {
    const { likeCount } = e.detail
    const boardData = this.data.boardData.map(item => {
      item.likeCount = likeCount
      return item
    })
    this.setData({ boardData })
  },

  /**
   * 处理动态项点击事件
   */
  onDynamicTap: function(e) {
    const item = e.currentTarget.dataset.item
    
    wx.showModal({
      title: item.title,
      content: '作者：' + item.author + '\n发布时间：' + item.publishTime + '\n类型：' + item.type,
      confirmText: '查看详情',
      cancelText: '取消',
      success: function(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/detail/index?id=' + item.id,
            fail: function(err) {
              console.error('页面跳转失败:', err)
              wx.showToast({
                title: '页面跳转失败',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },

  /**
   * 处理精选推荐点击事件
   */
  onBoardTap: function(e) {
    const item = e.currentTarget.dataset.item
    
    // 构建弹窗内容
    let content = '类型：' + (item.type || '未知') + '\n';
    if (item.tags && Array.isArray(item.tags)) {
      content += '标签：' + item.tags.join('、') + '\n';
    }
    content += '点赞数：' + (item.likeCount || 0);
    
    wx.showModal({
      title: item.title || item.content || '详情',
      content: content,
      confirmText: '查看详情',
      cancelText: '取消',
      success: function(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/detail/index?id=' + item.id,
            fail: function(err) {
              console.error('页面跳转失败:', err)
              wx.showToast({
                title: '页面跳转失败',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  }
})
